import { defineStore } from 'pinia';
import { yearlyFortune } from '@/services/apis';
import { isEmpty } from 'lodash'

export const useYearlyFortuneStore = defineStore('yearlyFortune', {
    state: () => ({
        fortuneTypeList: []
    }),
    actions: {
        async add(params: any) {
            await yearlyFortune.add(params);
        },
        async list(params: any) {
            try {
                await this.getFortuneTypeList();
                return await yearlyFortune.list({
                    orderByColumn: 'date',
                    isAsc: 'desc',
                    ...params
                });
            } catch (error) {
                return []
            }
        },
        async edit(params: any) {
            await yearlyFortune.edit(params);
        },
        async getFortuneTypeList() {
            if (!isEmpty(this.fortuneTypeList)) {
                return this.fortuneTypeList
            }
            const res = await yearlyFortune.lxyd_fortune_type({})
            this.fortuneTypeList = res
            return res
        },
        async save(params: any) {
            if (params.type === 'add') {
                await this.add([params])
            } else {
                await this.edit(params)
            }
        },
        async clean() {
            await yearlyFortune.clean()
        }
    }
})